"ui";
ui.layout(
    <vertical>
        <webview id="webView" h="*" w="auto"/>
    </vertical>
);

ui.webView.webViewClient = new JavaAdapter(android.webkit.WebViewClient, {
    shouldInterceptRequest: (webView, request) => {
        try {
            let req = JSON.parse(JSON.stringify(request));
            let curUrl = (request.a && request.a.a) || (req.mRequest && req.mRequest.url);
            if (curUrl.match(/api\.m\.jd\.com\/client\.action\?functionId=findBeanIndex/)) {
                let cookieManager = android.webkit.CookieManager.getInstance();
                let url = "https://plogin.m.jd.com/cgi-bin/ml/islogin";
                let cookieStr = cookieManager.getCookie(url);
                if (cookieStr) {
                    let response = http.get(url, {
                        headers: {
                            cookie: cookieStr
                        }
                    });
                    let json = response.body.json();
                    if (json.islogin == '1') {
                        toastLog("京东登录成功");
                        events.broadcast.emit("jdLoginSuccess", true);
                        log("广播通知： jdLoginSuccess");
                        // ui.finish();
                    }
                }
            }
        } catch (e) {
            console.trace(e);
        }
    }
});
ui.webView.loadUrl("https://bean.m.jd.com");